var categoryPage = avalon.define({
    $id: "categoryPage",
    categorys: [],
    newRows: 0,
    id: '',
    isHas: function() {
        return false;
    },
    categorylist: []
});
mui.ready(function() {
    mui.init({
        pullRefresh: {
            container: "#category", //待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
            up: {
                height: 50, //可选.默认50.触发上拉加载拖动距离
                auto: true, //可选,默认false.自动上拉加载一次
                contentrefresh: $.i18n.prop('contentRefresh'), //可选，正在加载状态时，上拉加载控件上显示的标题内容
                contentnomore: $.i18n.prop('noData'), //可选，请求完毕若没有更多数据时显示的提醒内容；
                callback: function() { //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
                    setTimeout(function() {
                        mui.ajax('/product/index.json', {
                            dataType: 'json',
                            data: { cid: '0', index: "false" },
                            success: function(data) {
                                var i = sessionStorage.getItem('pb');
                                categoryPage.categorys = data.categorys;
                                // 给第X个li添加样式mui-li
                                setTimeout(() => {
                                    if (sessionStorage.getItem('pb') !== '0AI666') {
                                        $('.mui-table-view-cell')[i].className = 'mui-table-view-cell mui-li';
                                        categoryPage.categorylist = data.categorys[i].childs
                                    } else {
                                        $('.mui-table-view-cell')[0].className = 'mui-table-view-cell mui-li';
                                        categoryPage.categorylist = data.categorys[0].childs
                                    }
                                }, 30)
                            }
                        });
                        mui('#category').pullRefresh().endPullupToRefresh();
                    }, window.DAHUA_CONFIG.LOADING_TIME);
                }
            }
        }
    });
    mui('.mui-scroll-wrapper').scroll({
        scrollY: true, //是否竖向滚动
        scrollX: false, //是否横向滚动
        startX: 60, //初始化时滚动至x
        startY: -100, //初始化时滚动至y
        indicators: false, //是否显示滚动条
        deceleration: 0.0006, //阻尼系数,系数越小滑动越灵敏;flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
        bounce: true //是否启用回弹
    });

    mui('.mui-table-view').on('tap', 'li', function(to) {
        var siblings = this.parentNode.childNodes;
        for (var i = 0; i < siblings.length; i++) {
            if (siblings[i].nodeType == 1 && siblings[i] != this) {
                siblings[i].className = 'mui-table-view-cell';
            } else {
                siblings[i].className = 'mui-table-view-cell mui-li'
            }
        }
        var id = to.detail.target.id;
        mui.ajax('/category/getSubCategory.json', {
            dataType: 'json',
            success: function(data) {
                mui.each(data, function(i, item) {
                    if (item.id == id) {
                        categoryPage.categorylist = item.childs;
                    }
                })
                var scroll = mui(".list .mui-scroll-wrapper .mui-scroll")[0];
                scroll.style.transform = "translate3d(0px, 0px, 0px) translateZ(0px)";
            }
        })
    });
    mui(".mui-row").on('tap', 'a', function() {
        var url = this.getAttribute('href');
        // var id = this.getAttribute("id");
        mui.openWindow({
            url: url
        });
        console.log(url)
    });
});